/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MaterialesConsulta.java
 *
 * Created on 15-jul-2011, 23:02:44
 */
package Presentacion.AbmClientes;

import Negocio.Modelo.Cliente;
import Negocio.ProcesarClientes;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Dan
 */
public class ClientesConsulta extends javax.swing.JPanel {

    /** Creates new form MaterialesConsulta */
    private DefaultTableModel dtm;
    private String[] columnas = {"Cuit", "Nombre", "Direccion", "Codigo Postal", "Telefono", "Fax", "Dado de Baja"};
    private Cliente cliente;
    
    /*Indica la etapa en que esta la operacion
     *0 = Iniciada
     *1 = Para dar de alta
     *2 = Para dar de baja
     *3 = Para modificar
     */
    private Integer etapa = 0;
    private Integer pasoModificar = 1;
    
    public ClientesConsulta() {
        dtm = new DefaultTableModel(columnas, 0);
        initComponents();
        btnClienteModificar.setEnabled(false);
        btnClienteAlta.setEnabled(false);
        btnClienteBaja.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtClienteCuit = new javax.swing.JTextField();
        txtClienteNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtClienteDireccion = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtClienteCodPost = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtClienteTelefono = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtClienteFax = new javax.swing.JTextField();
        lblClienteBajaDesc = new javax.swing.JLabel();
        lblClienteBaja = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBusquedaCliente = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        btnClienteBuscar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnClienteAlta = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnClienteBaja = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        btnClienteModificar = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnClienteCancelar = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(Presentacion.SIPGUIApp.class).getContext().getResourceMap(ClientesConsulta.class);
        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtClienteCuit.setText(resourceMap.getString("txtClienteCuit.text")); // NOI18N
        txtClienteCuit.setName("txtClienteCuit"); // NOI18N

        txtClienteNombre.setText(resourceMap.getString("txtClienteNombre.text")); // NOI18N
        txtClienteNombre.setName("txtClienteNombre"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtClienteDireccion.setName("txtClienteDireccion"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txtClienteCodPost.setName("txtClienteCodPost"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtClienteTelefono.setName("txtClienteTelefono"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        txtClienteFax.setName("txtClienteFax"); // NOI18N
        txtClienteFax.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtClienteFaxActionPerformed(evt);
            }
        });

        lblClienteBajaDesc.setText(resourceMap.getString("lblClienteBajaDesc.text")); // NOI18N
        lblClienteBajaDesc.setName("lblClienteBajaDesc"); // NOI18N

        lblClienteBaja.setText(resourceMap.getString("lblClienteBaja.text")); // NOI18N
        lblClienteBaja.setName("lblClienteBaja"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtClienteCodPost, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtClienteTelefono, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                    .addComponent(txtClienteCuit, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtClienteDireccion, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtClienteFax, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblClienteBajaDesc)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblClienteBaja))
                    .addComponent(txtClienteNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtClienteCuit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtClienteNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtClienteCodPost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtClienteDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtClienteTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtClienteFax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(lblClienteBajaDesc)
                    .addComponent(lblClienteBaja))
                .addContainerGap())
        );

        jScrollPane1.setName("tblBusquedaMaterial"); // NOI18N

        tblBusquedaCliente.setModel(this.dtm);
        tblBusquedaCliente.setName("jscroll"); // NOI18N
        tblBusquedaCliente.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblBusquedaCliente);

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        btnClienteBuscar.setText(resourceMap.getString("btnClienteBuscar.text")); // NOI18N
        btnClienteBuscar.setName("btnClienteBuscar"); // NOI18N
        btnClienteBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClienteBuscarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnClienteBuscar);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        btnClienteAlta.setText(resourceMap.getString("btnClienteAlta.text")); // NOI18N
        btnClienteAlta.setFocusable(false);
        btnClienteAlta.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnClienteAlta.setName("btnClienteAlta"); // NOI18N
        btnClienteAlta.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnClienteAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClienteAltaActionPerformed(evt);
            }
        });
        jToolBar1.add(btnClienteAlta);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        btnClienteBaja.setText(resourceMap.getString("btnClienteBaja.text")); // NOI18N
        btnClienteBaja.setFocusable(false);
        btnClienteBaja.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnClienteBaja.setName("btnClienteBaja"); // NOI18N
        btnClienteBaja.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnClienteBaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClienteBajaActionPerformed(evt);
            }
        });
        jToolBar1.add(btnClienteBaja);

        jSeparator5.setName("jSeparator5"); // NOI18N
        jToolBar1.add(jSeparator5);

        btnClienteModificar.setText(resourceMap.getString("btnClienteModificar.text")); // NOI18N
        btnClienteModificar.setFocusable(false);
        btnClienteModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnClienteModificar.setName("btnClienteModificar"); // NOI18N
        btnClienteModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnClienteModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClienteModificarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnClienteModificar);

        jSeparator3.setName("jSeparator3"); // NOI18N
        jToolBar1.add(jSeparator3);

        btnClienteCancelar.setText(resourceMap.getString("btnClienteCancelar.text")); // NOI18N
        btnClienteCancelar.setName("btnClienteCancelar"); // NOI18N
        btnClienteCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClienteCancelarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnClienteCancelar);

        jSeparator4.setName("jSeparator4"); // NOI18N
        jToolBar1.add(jSeparator4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 566, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void setCompletarTabla(Cliente cli) {

        ArrayList<Cliente> clientes = new ProcesarClientes().getCliente(cli);
        Iterator<Cliente> itrCli = clientes.iterator();
        //Limpio la tabla
        this.limpiarTabla();
        //Agrego los clientes encontrados a la tabla
        while (itrCli.hasNext()) {
            Cliente client = itrCli.next();
            dtm.addRow(new Object[]{client.getCuit(),
                        client.getNombre(),
                        client.getDireccion(),
                        client.getCodigoPostal(),
                        client.getTelefono(),
                        client.getFax(),
                        (client.getHabilitado()) ? "No" : "Si"
                    });
        }
    }

    public void limpiarTabla() {
        dtm.setRowCount(0);
    }

    private void cargarNuevoCliente() {
        //Indico que estoy en la etapa de Alta del cliente
        this.etapa = 1;

        //Deshabilito el boton para buscar y habilito el de alta
        btnClienteBuscar.setEnabled(false);
        btnClienteAlta.setEnabled(false);
        btnClienteAlta.setEnabled(true);
        lblClienteBajaDesc.setEnabled(false);
        lblClienteBaja.setText("");
        /*Deshabilito el txt cuit para que no lo modifiquen (solo en caso que lo
         * hayan cargado anteriormente
         */
        if (cliente.getCuit() != 0) {
            txtClienteCuit.setEditable(false);
            txtClienteNombre.requestFocus();
        }

    }

    private void limpiarCampos() {
        txtClienteCodPost.setText("");
        txtClienteCuit.setText("");
        txtClienteCuit.requestFocus();
        txtClienteDireccion.setText("");
        txtClienteFax.setText("");
        txtClienteNombre.setText("");
        txtClienteTelefono.setText("");
        btnClienteAlta.setEnabled(false);
        btnClienteModificar.setEnabled(false);
        btnClienteBaja.setEnabled(false);
        lblClienteBajaDesc.setEnabled(false);
        lblClienteBaja.setText("");
        this.limpiarTabla();
        this.etapa = 0;
    }

    private void setTextEnObCliente() {
        if (!txtClienteCuit.getText().isEmpty()) {
            cliente.setCuit(Long.parseLong(txtClienteCuit.getText()));
        }
        cliente.setNombre(txtClienteNombre.getText());
        cliente.setDireccion(txtClienteDireccion.getText());
        cliente.setCodigoPostal(txtClienteCodPost.getText());
        cliente.setTelefono(txtClienteTelefono.getText());
        cliente.setFax(txtClienteFax.getText());

    }

    private void setFormAlEstadoDeBusqueda() {
        this.etapa = 0;
        this.limpiarCampos();
        txtClienteCuit.setEditable(true);
        txtClienteCuit.requestFocus();
        btnClienteBuscar.setEnabled(true);
        btnClienteAlta.setEnabled(false);
        btnClienteBaja.setEnabled(false);
    }

    private boolean validarEtapa() {
        String message = "Se detectaron los siguientes errores:\n";
        Boolean error = false;
        switch (this.etapa) {
            //Validaciones para la busqueda de un cliente
            case 0: {
                message = "Debe ingresar al menos una opcion";
                error = true;
                JOptionPane.showMessageDialog(this, message, "Buscar Cliente", JOptionPane.WARNING_MESSAGE);
            }
            break;
            //Validaciones para dar de alta un cliente
            case 1: {
                this.setTextEnObCliente();
                if (cliente.getCuit() == 0) {
                    message += "-El Cuit es un campo obligatorio.\n";
                    error = true;
                }

                if (cliente.getNombre().isEmpty()) {
                    message += "-El Nombre es un campo obligatorio.\n";
                    error = true;
                }

                if (cliente.getCodigoPostal().isEmpty()) {
                    message += "-El CP es un campo obligatorio.\n";
                    error = true;
                }

                if (cliente.getDireccion().isEmpty()) {
                    message += "-La Direccion es un campo obligatorio.\n";
                    error = true;
                }

                if (cliente.getTelefono().isEmpty()) {
                    message += "-El Telefono es un campo obligatorio.\n";
                    error = true;
                }

                if (cliente.getFax().isEmpty()) {
                    message += "-El Fax es un campo obligatorio.\n";
                    error = true;
                }

                if (error) {
                    JOptionPane.showMessageDialog(this, message, "Alta Cliente", JOptionPane.WARNING_MESSAGE);
                }
            }
            break;
            //Validaciones para la modificacion de un cliente
            case 2: {
                System.out.println("Valido la modificacion");
                error = true;
            }
            break;
        }
        return error;
    }

    private void btnClienteBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClienteBuscarActionPerformed
        cliente = new Cliente();
        Integer option = null;
        this.setTextEnObCliente();

        if (cliente.isEmpty()) {
            this.validarEtapa();
        } else {
            if (new ProcesarClientes().existeCliente(cliente)) {
                this.setCompletarTabla(cliente);
                btnClienteBaja.setEnabled(true);
                btnClienteModificar.setEnabled(true);

            } else {
                String message = "El cliente solicitado no existe. Quiere darlo de alta?";
                option = JOptionPane.showConfirmDialog(this, message, "Clientes", JOptionPane.WARNING_MESSAGE);
                //El sistema actua de acuerdo a la seleccion del usuario
                if (option == 0) {
                    this.cargarNuevoCliente();
                } else {
                    this.limpiarCampos();
                }
            }
        }
    }//GEN-LAST:event_btnClienteBuscarActionPerformed

    private void txtClienteFaxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtClienteFaxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtClienteFaxActionPerformed

    /**
     * @param Evento
     *  @see Se ejecutan todas las acciones del boton de cancelar Cliente
     */
    private void btnClienteCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClienteCancelarActionPerformed
        Integer option;
        switch (this.etapa) {
            //Cancelacion de la busqueda
            case 0: {
                this.limpiarCampos();
            }
            break;
            //Cancelacion del alta
            case 1: {
                //Pregunto si quiere cancelar el alta
                String message = "Esta seguro que quiere cancelar el alta de este Cliente?";
                option = JOptionPane.showConfirmDialog(this, message, "Alta Cliente", JOptionPane.WARNING_MESSAGE);
                //El sistema actua de acuerdo a la seleccion del usuario
                if (option == 0) {
                    this.setFormAlEstadoDeBusqueda();
                }
            }
            break;
            //Cancelar la baja
            case 2: {
                String message = "Esta seguro que quiere cancelar la baja de este Cliente?";
                option = JOptionPane.showConfirmDialog(this, message, "Baja Cliente", JOptionPane.WARNING_MESSAGE);
                //El sistema actua de acuerdo a la seleccion del usuario
                if (option == 0) {
                    this.setFormAlEstadoDeBusqueda();
                }
            }
            break;
            //Cancelar luego de la modificacion Modificacion
            case 3: {
                this.setFormAlEstadoDeBusqueda();
            }
            break;
        }
    }//GEN-LAST:event_btnClienteCancelarActionPerformed
    /**
     * @param Evento.
     *  @see Se ejecutan todas las acciones del boton de Alta de Cliente
     */
    private void btnClienteAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClienteAltaActionPerformed
        if (!this.validarEtapa()) {
            this.setTextEnObCliente();
            ProcesarClientes proCli = new ProcesarClientes();
            proCli.agregarCliente(cliente);
            if (proCli.existeCliente(cliente)) {
                String message = "El cliente " + cliente.getCuit() + " se dio de alta satisfactoriamente.";
                JOptionPane.showConfirmDialog(this, message, "Alta Cliente", JOptionPane.WARNING_MESSAGE);
                btnClienteAlta.setEnabled(false);
                btnClienteBuscar.setEnabled(true);
                cliente = new Cliente();
                this.limpiarCampos();
                txtClienteCuit.setEnabled(true);
                this.etapa = 0;
            } else {
                String message = "El cliente " + cliente.getCuit() + " no pudo darse de alta satisfactoriamente.";
                JOptionPane.showConfirmDialog(this, message, "Alta Cliente", JOptionPane.WARNING_MESSAGE);
            }
        }


    }//GEN-LAST:event_btnClienteAltaActionPerformed
    /**
     * @param evento
     * @see Se ejecutan todas las acciones del boton de baja del cliente
     * 
     */
    private void btnClienteBajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClienteBajaActionPerformed
        this.etapa = 3;
        int row = tblBusquedaCliente.getSelectedRow();
        if (row < 0) {
            String message = "Debe seleccionar el cliente que desea dar de baja en la lista";
            JOptionPane.showMessageDialog(this, message, "Baja Cliente", JOptionPane.WARNING_MESSAGE);
        } else {
            String message = "Confirma Baja de cliente: " + dtm.getValueAt(row, 1) + "?";
            int option = JOptionPane.showConfirmDialog(this, message, "Baja Cliente", JOptionPane.WARNING_MESSAGE);
            if (option == 0) {
                if (dtm.getValueAt(row, 6).toString().equals("Si")) {
                    message = "Este Cliente ya esta dado de baja";
                    JOptionPane.showMessageDialog(this, message, "Baja Cliente", JOptionPane.WARNING_MESSAGE);
                } else {
                    //Saco los datos de la fila seleccionada y lo pasoModificar a objeto
                    cliente.setCuit(Long.parseLong(dtm.getValueAt(row, 0).toString()));
                    cliente.setNombre(dtm.getValueAt(row, 1).toString());
                    cliente.setDireccion(dtm.getValueAt(row, 2).toString());
                    cliente.setCodigoPostal(dtm.getValueAt(row, 3).toString());
                    cliente.setTelefono(dtm.getValueAt(row, 4).toString());
                    cliente.setFax(dtm.getValueAt(row, 5).toString());
                    if (dtm.getValueAt(row, 6).toString().equals("No")) {
                        cliente.setHabilitado(true);
                    } else {
                        cliente.setHabilitado(false);
                    }
                    ProcesarClientes procCli = new ProcesarClientes();
                    procCli.bajaCliente(cliente);
                    this.setFormAlEstadoDeBusqueda();
                }

            }


        }

    }//GEN-LAST:event_btnClienteBajaActionPerformed

    private void btnClienteModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClienteModificarActionPerformed
        this.etapa = 4;
        int row = tblBusquedaCliente.getSelectedRow();
        
        if (this.pasoModificar == 1) {
            if (row < 0) {
                String message = "Debe seleccionar el cliente que desea modificar en la lista";
                JOptionPane.showMessageDialog(this, message, "Modificar Cliente", JOptionPane.WARNING_MESSAGE);
            } else {
                String message = "Confirma Modificacion de cliente: " + dtm.getValueAt(row, 1) + "?";
                int option = JOptionPane.showConfirmDialog(this, message, "Modificar Cliente", JOptionPane.WARNING_MESSAGE);
                if (option == 0) {
                    //Saco los datos de la fila seleccionada y lo pasoModificar a textbox
                    lblClienteBajaDesc.setEnabled(true);
                    btnClienteBaja.setEnabled(false);
                    txtClienteCuit.setText(dtm.getValueAt(row, 0).toString());
                    txtClienteNombre.setText(dtm.getValueAt(row, 1).toString());
                    txtClienteDireccion.setText(dtm.getValueAt(row, 2).toString());
                    txtClienteCodPost.setText(dtm.getValueAt(row, 3).toString());
                    txtClienteTelefono.setText(dtm.getValueAt(row, 4).toString());
                    txtClienteFax.setText(dtm.getValueAt(row, 5).toString());
                    lblClienteBaja.setText(dtm.getValueAt(row, 6).toString());
                    lblClienteBaja.setText(dtm.getValueAt(row, 6).toString());
                    this.pasoModificar = 2;

                }

            }
        } else {
            this.setTextEnObCliente();
            ProcesarClientes procCli = new ProcesarClientes();
            procCli.modificarCliente(cliente);
            this.setFormAlEstadoDeBusqueda();
            this.pasoModificar = 1;
        }
    }//GEN-LAST:event_btnClienteModificarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClienteAlta;
    private javax.swing.JButton btnClienteBaja;
    private javax.swing.JButton btnClienteBuscar;
    private javax.swing.JButton btnClienteCancelar;
    private javax.swing.JButton btnClienteModificar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblClienteBaja;
    private javax.swing.JLabel lblClienteBajaDesc;
    private javax.swing.JTable tblBusquedaCliente;
    private javax.swing.JTextField txtClienteCodPost;
    private javax.swing.JTextField txtClienteCuit;
    private javax.swing.JTextField txtClienteDireccion;
    private javax.swing.JTextField txtClienteFax;
    private javax.swing.JTextField txtClienteNombre;
    private javax.swing.JTextField txtClienteTelefono;
    // End of variables declaration//GEN-END:variables
}
